package trinnes.oliver.unimaas.smartpentalath.control;

import java.awt.Window;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import trinnes.oliver.unimaas.smartpentalath.data.Move;
import trinnes.oliver.unimaas.smartpentalath.data.Piece;
import trinnes.oliver.unimaas.smartpentalath.data.Validation;

/**
 *
 * @author Oliver Trinnes
 */
public class HumanPlayer extends Player
{

  public HumanPlayer(Game game, Piece piece, String name, int ID)
  {
    super(game, piece, name, ID);
  }

  @Override
  public int getNextMove()
  {
    if(game.getValidation(Move.SWAP, getPiece()).equals(Validation.VALID))
    {
      int answer = JOptionPane.showConfirmDialog(Window.getWindows()[0], "Would player 2 like to swap the colors?", "Swap Option", JOptionPane.YES_NO_OPTION);
      if(answer == JOptionPane.YES_OPTION)
        return Move.SWAP;
    }
    while (true)
    {
      while (true)
      {
        try
        {
          Thread.sleep(100);
        }
        catch (InterruptedException ex)
        {
          Logger.getLogger(HumanPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (nextMoveChanged)
          break;
      }
      nextMoveChanged = false;
      if(getGame().getValidation(nextMove, getPiece()).equals(Validation.VALID))
        return nextMove;
      else if(getGame().getValidation(nextMove, getPiece()).equals(Validation.SUICIDE))
        JOptionPane.showMessageDialog(null, "No suicide allowed.", "Invalid move", JOptionPane.WARNING_MESSAGE);
        
      else if(getGame().getValidation(nextMove, getPiece()).equals(Validation.OCCUPIED))
        JOptionPane.showMessageDialog(null, "Selected cell is not empty.", "Invalid move", JOptionPane.WARNING_MESSAGE);
    }
  }


}
